Imports FetchXML
Imports FetchXMLBuilder.MetadataServiceSdk

Public Class frmAttribute

    Private Entity As Entity

    Public Sub New(ByVal Entity As Entity)
        InitializeComponent()
        Me.Entity = Entity
    End Sub

    Private Sub frmAttribute_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim i As Integer
        Dim MetaEntity As EntityMetadata
        MetaEntity = GetEntity(Entity.Name)
        For i = 0 To MetaEntity.Attributes.Length - 1
            Me.lstMetaAttrib.Items.Add(MetaEntity.Attributes(i).Name)
        Next
        Dim doc As New Xml.XmlDocument
        Dim attrib, tempattrib As Attribute
        For Each attrib In Entity.Attributes
            doc.LoadXml(attrib.ToXML)
            tempattrib = New Attribute(doc.DocumentElement)
            Me.lstAttrib.Items.Add(tempattrib)
        Next
        me.lstMetaAttrib.Sorted=True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Entity.Attributes.Clear()
        Dim attrib As Object
        For Each attrib In Me.lstAttrib.Items
            Me.Entity.Attributes.Add(CType(attrib, Attribute))
        Next
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim metaattrib As String
        Dim attrib As Attribute
        Dim found As Boolean
        For Each metaattrib In Me.lstMetaAttrib.SelectedItems
            found = False
            For Each attrib In Me.lstAttrib.Items
                If attrib.Name = metaattrib Then
                    found = True
                    Exit For
                End If
            Next
            If Not found Then
                Me.lstAttrib.Items.Add(New Attribute(metaattrib))
            End If
        Next
    End Sub

    Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        Me.lstAttrib.Items.Remove(Me.lstAttrib.SelectedItem)
    End Sub

    Private Sub lstAttrib_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstAttrib.DoubleClick
        Dim f As New frmAttributeProperty(Me.lstAttrib.Items(Me.lstAttrib.SelectedIndex))
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            refreshattributes()
        End If
    End Sub

    Private Sub btnProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProperties.Click
        Dim f As New frmAttributeProperty(Me.lstAttrib.Items(Me.lstAttrib.SelectedIndex))
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            refreshattributes()
        End If
    End Sub

    Private Sub PropertiesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PropertiesToolStripMenuItem.Click
        Dim f As New frmAttributeProperty(Me.lstAttrib.Items(Me.lstAttrib.SelectedIndex))
        If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
            refreshattributes()
        End If
    End Sub

    Private Sub refreshattributes()
        Dim col As New Attributes
        Dim attrib As Attribute
        For Each attrib In Me.lstAttrib.Items
            col.Add(attrib)
        Next
        Me.lstAttrib.Items.Clear()
        Dim i As Integer
        For i = 0 To col.Count - 1
            Me.lstAttrib.Items.Add(col(i))
        Next
    End Sub

End Class